package com.ailesson.video.controller;

import java.util.Arrays;
import java.util.List;

import com.ailesson.video.model.dto.ExcErrorInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ailesson.video.model.reply.SubjectReply;
import com.ailesson.video.model.req.SubjectReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.ailesson.video.service.SubjectService;
import com.ailesson.video.model.req.SubjectQueryReq;
import com.ailesson.common.reply.PageResult;
import com.ailesson.common.reply.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * 课程科目
 *
 * @author mac
 * @email mac@gmail.com
 * @date 2024-09-30 23:08:10
 */
@Tag(name = "课程科目模块")
@RestController
@RequestMapping("/admin/video/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    /**
     * 列表
     */
    @PostMapping("/list")
    @Operation(summary = "列表")
    @Parameter(name = "params", in = ParameterIn.DEFAULT, description = "查询参数")
   // @RequiresPermissions("video:subject:list")
    public R list(@RequestBody SubjectQueryReq params){
        PageResult page = subjectService.queryPage(params);

        return R.success(page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @Operation(summary = "获取信息")
    @Parameter(name = "id",in = ParameterIn.PATH, description = "ID")
   // @RequiresPermissions("video:subject:info")
    public R info(@PathVariable("id") Long id){
		SubjectReply subject = subjectService.info(id);

         return R.success(subject);
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    @Operation(summary = "添加")
    @Parameter(name = "teacher",in = ParameterIn.QUERY, description = "添加信息")
    //@RequiresPermissions("video:subject:save")
    public R add(@RequestBody SubjectReq subject){
        return R.success(subjectService.add(subject));
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @Operation(summary = "更新")
    @Parameter(name = "teacher",in = ParameterIn.QUERY, description = "更新信息")
    //@RequiresPermissions("video:subject:update")
    public R update(@RequestBody SubjectReq subject){
		subjectService.update(subject);

        return R.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除")
    @Parameter(name = "ids",in = ParameterIn.QUERY, description = "删除主键ids")
    //@RequiresPermissions("video:subject:delete")
    public R delete(@RequestBody Long[] ids){
		subjectService.removeByIds(Arrays.asList(ids));

        return R.success();
    }

    @Operation(summary = "查询子节点")
    @GetMapping("/getChildSubject/{pid}")
    @Parameter(name = "pid", in= ParameterIn.PATH, description = "父级ID")
    public R getChildSubject(@PathVariable Long pid){
        return R.success(subjectService.getChildSubject(pid));
    }

    @Operation(summary = "导出")
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response) {
        subjectService.exportData(response);
    }

    @Operation(summary = "课程分类导入")
    @Parameter(name = "file",in = ParameterIn.HEADER, description = "文件")
    @PostMapping("/importData")
    public R importData(@RequestParam("file") MultipartFile file) {
        List<ExcErrorInfoDto> excErrorInfoDtos = subjectService.importData(file);
        return R.success(excErrorInfoDtos);
    }

}
